import { z } from 'zod'
import type { Request } from 'express'
import { todoService } from '@server/services/todo-service'
import { ApiResponse } from '@data/api'
import { ParameterError } from '@server/error/errors'

// 导出参数验证 schema
export const bodySchema = z.object({
  title: z.string().min(1, 'Title is required'),
  description: z.string().optional(),
  priority: z.enum(['high', 'medium', 'low', 'none']).optional(),
  tags: z.array(z.string()).optional(),
  groupId: z.string().optional(),
  dueDate: z.string().optional(),
})

export default async function createTodo(request: Request, data: z.infer<typeof bodySchema>) {
  try {
    const todo = await todoService.createTodo(data)

    const response: ApiResponse = {
      success: true,
      data: todo,
      message: 'Todo created successfully',
    }

    return response
  } catch (error) {
    throw new ParameterError(error instanceof Error ? error.message : 'Failed to create todo')
  }
}
